/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package htmlextract;

import javax.swing.text.MutableAttributeSet;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTMLEditorKit;
/**
 *
 * @author Fidschi
 */

// Implement the call back class. Just like a SAX content handler
public class Callback extends HTMLEditorKit.ParserCallback
{
    final StringBuffer buf = new StringBuffer(2000);
    private int anzTabellen = 0;
    HTML.Tag tg = null;

    @Override
    public void handleStartTag(HTML.Tag t, MutableAttributeSet a, int pos) {
        tg = t;
        if (t == HTML.Tag.TABLE)
        {
            this.setAnzTabellen(anzTabellen+1);
            buf.append("Tabelle" +Integer.toString(getAnzTabellen())+ ";;");
        }
        else if (tg == HTML.Tag.TD)
        {
            buf.append(";;");
        }
    }

    public void setAnzTabellen(int anzTabellen) {
        this.anzTabellen = anzTabellen;
    }

    public int getAnzTabellen() {
        return anzTabellen;
    }

    @Override
    public void handleEndTag(HTML.Tag t, int pos) {
        //if (t == HTML.Tag.TR || t == HTML.Tag.TH) System.out.println("\n");
        //if (t == HTML.Tag.TD) System.out.println("\t");
    }

    // This method is called whenever text is encountered in the HTML file
    @Override
    public void handleText(char[] data, int pos) {
        if (tg == HTML.Tag.TD)
        {
            //System.out.println(data);
            buf.append(data);
            //buf.append(";;");
        }
    }

    public String getBuf() {
    	buf.append(";;");
        return buf.toString();
    }
}
